export const request = (url, method, params) => {
	const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : '';
	uni.showLoading({
		title: "加载中"
	});
	let data = {
		url: "https://aopu.huijipin.cn/api/" + url,
		method: method,
		data: {
			...params,
		},
	}
	return new Promise((resolve, reject) => {
		uni.request({
			...data,
			header: {
				token: token
			},
			success(res) {
				const {
					code,
					msg
				} = res.data;
				switch (code) {
					case 0:
						uni.showToast({
							title: msg,
							icon: "error",
						});
						break;
						//信息不全
					case 4:
						uni.showToast({
							title: msg,
							icon: "error",
						});
						setTimeout(() => {
							uni.navigateTo({
							})
						}, 500)
						break;
						//没有绑定微信
					case 3:
						uni.showToast({
							title: msg,
							icon: "error",
						});
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/index/my/zhanghao'
							})
						}, 500)
						break;
					case 401: //错误码token
						uni.showToast({
							title: '请登录',
							icon: "error",
						});
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}, 500)
						break;
					default:
						resolve(res.data);
						uni.hideLoading();
						break;
				}
			},
			fail(err) {
				reject(err);
				uni.hideLoading();
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}
export const api = (url, params, qingqiu) => { //主性格
	let a = qingqiu ? qingqiu : 'post'
	return request(url, a, params)
}
